/*
 * SWLcd.c
 *
 * Created: 2013-02-02 17:06:35
 *  Author: ant
 */ 

#include "SWLcd.h"
#include <stdlib.h>
#include <util/delay.h>

#define TOUT1	15
#define TOUT2	15
#define WUS		50

u8 swl_check()
{
	if (PINB & (1 << LINE))
		return 1;
	return 0;
}

void send_debug_word(u16 word)
{/*
	for (u8 c = 0; c < 12; c++)
	{
		LINE_PULL;
		if (word & (1 << c))
		{
			LINE_LEAVE;
		}			
		_delay_us(40);
		LINE_LEAVE;
		_delay_us(80);
	}
	*/

	LINE_PULL;
	if (word & (1 << 0))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 1))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 2))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 3))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 4))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 5))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 6))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 7))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 8))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 9))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	LINE_PULL;
	if (word & (1 << 10))
	LINE_LEAVE;
	_delay_us(TOUT1);
	LINE_LEAVE;
	_delay_us(TOUT2);
	
	
}

void swl_proceed()
{
	u16 word = 0;
	send_debug_word(word);
}

void swl_clear()
{
	u16 word = (1 << 8) | 1;
	send_debug_word(word);		// flags 1 & rbyte 1
	_delay_us(WUS);
}

void swl_home()
{
	u16 word = (1 << 8) | 2;
	send_debug_word(word);		// flags 1 & rbyte 2
	_delay_us(WUS);
}


void swl_setpos(u8 pos)
{
	pos |= 0x80;
	u16 word = (1 << 8) | pos;
	send_debug_word(word);
}

void swl_print(char *text)
{

	u8 len = strlen(text);
	for (u8 c = 0; c < len; c++)
	{
		send_debug_word(text[c]);
		_delay_us(WUS);
	}		
		
}

void swl_printi(u16 val)
{
	char str[8];
	itoa(val,str,10);
	u8 len = strlen(str);
	for (u8 c = 0; c < len; c++)
	{
		send_debug_word(str[c]);
		_delay_us(WUS);
	}
	
}

void swl_led_on(u8 led)
{
	
	
	
}

